//
//  StatusModel.swift
//  weibobbaj
//
//  Created by Tiffany宝 on 16/4/20.
//  Copyright © 2016年 Tiffany. All rights reserved.
//

import UIKit

class StatusModel: NSObject {
    
    //原创微博的正文内容
    var text: String?
    
    var source: String?
    
    var created_at: String?
    
    //用户微博
    var user: UserModel?
    
    // 当前微博转发微博，因为其内部字段与 StatusModel 一样，所以就设置其类型为 StatusModel
    var retweeted_status : StatusModel?
    
    //微博配图
    var pic_urls: [PictureModel]?
//        {
//        didSet{
//            //测试 图片为4张时 的显示效果
//            if let pic = pic_urls where pic.count >= 4{
//                
//                //如果图片数量大于4,就截取前4个
//               pic_urls = (pic as NSArray).subarrayWithRange(NSMakeRange(0, 4)) as? [PictureModel]
//                
//            }
//        }
//    }
    
    //微博id
//    var id: Int64?
    var id: Int64 = 0 //基本数据类型的初始化
    
    init(dic:[String : AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dic)
        
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        
        if key == "user"{
            
            guard let dic = value as? [String : AnyObject] else{
                
                return
            }
            // 把 user 字典转模型转成 UserModel
            //            setValuesForKeysWithDictionary(dic)
            user = UserModel(dic: dic)
            
        }else if  key == "retweeted_status"{
            
            guard let dic = value as? [String :AnyObject] else{
                
                return
            }
            retweeted_status = StatusModel(dic: dic)
            
        }else if key == "pic_urls"{
            
            guard let dics = value as? [[String : AnyObject]] else{
                
                return
            }
            
            //把模型加到临时数组中
            var tempArr = [PictureModel]()
            
            //遍历数组 -> 字典转模型
            for dic in dics{
                
                let info = PictureModel(dic: dic)
                tempArr.append(info)
            }
            //赋值回去
            pic_urls = tempArr
            
        }else{
            
            super.setValue(value, forKey: key)
        }
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    //   技巧 ---- [].description
    
    override var description: String{
        
        let keys = ["text","source","created_at","user","retweeted_status","pic_urls"]
        
        //模型转字典
        return dictionaryWithValuesForKeys(keys).description
    }
    
}
